package com.heima.controller;

import com.heima.expection.BusinessException;
import com.heima.expection.SystemException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionAdviceHandler {
    @ExceptionHandler(BusinessException.class)
    public Result exception(BusinessException e){
        System.out.println("BusinessException = " + e.getMessage());
        e.printStackTrace();
        return new Result(e.getCode(),null,e.getMessage());
    }

    @ExceptionHandler(SystemException.class)
    public Result exception(SystemException e){
        System.out.println("SystemException = " + e.getMessage());
        e.printStackTrace();
        return new Result(e.getCode(),null,e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        System.out.println("Exception = " + e.getMessage());
        e.printStackTrace();
        return new Result(Code.ERR_ERR,null,"未知错误");
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public Result dive(DataIntegrityViolationException e){
        String msg = "首先";
        if (e.getMessage().contains("Data too long")){
            msg = "其次";
        }
        return new Result(Code.DAO_ERR,null,msg);
    }
}
